/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user;

import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserEditDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserLoginDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.add.in.AddUserEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.in.EditUserEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.edit.in.UpdateUserEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.login.in.LoginEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.logout.in.LogoutEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.remove.in.RemoveUserEvent;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.event.IEventListener;
import com.googlecode.hmvc4java.registry.controller.AControllerRegistry;
import com.googlecode.hmvc4java.registry.eventregistry.events.in.AddEventRegistryListenerEvent;
import com.googlecode.hmvc4java.registry.eventregistry.events.in.RemoveEventRegistryListenerEvent;

/**
 * 
 * @author jens.meiss
 */
public class UserRemoteServiceController extends AControllerRegistry {
	
	private void addUser(UserNewDTO userNewDTO, IEventListener eventListener) {
		UserAsyncCallback.addUser(userNewDTO, eventListener);
	}
	
	/**
	 * @param eventListener
	 */
	private void editUser(IEventListener eventListener) {
		
		UserAsyncCallback.edit(eventListener);
		
	}

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == LoginEvent.class) {
			
			login((((LoginEvent) event).getUserLoginDTO()), (((LoginEvent) event).getEventListener()));
			
		} else if (event.getClass() == LogoutEvent.class) {
			
			logout(((LogoutEvent) event).geteEventListener());
			
		} else if (event.getClass() == RemoveUserEvent.class) {
			
			removeUser(((LogoutEvent) event).geteEventListener());
			
		} else if (event.getClass() == AddUserEvent.class) {
			
			addUser(((AddUserEvent) event).getUserNewDTO(), ((AddUserEvent) event).getEventListener());
			
		} else if (event.getClass() == EditUserEvent.class) {
			
			editUser(((EditUserEvent) event).getEventListener());
			
		} else if (event.getClass() == UpdateUserEvent.class) {
			
			updateUser(((UpdateUserEvent) event).getEditDTO(), ((UpdateUserEvent) event).getEventListener());
			
		}
		
	}

	public void init() {
		
		fireEventGlobal(new AddEventRegistryListenerEvent(LoginEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(LogoutEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(AddUserEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(RemoveUserEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(EditUserEvent.class, this));
		fireEventGlobal(new AddEventRegistryListenerEvent(UpdateUserEvent.class, this));
		
		
	}
	
	private void login(UserLoginDTO userLoginDTO, IEventListener eventListener) {		
		UserAsyncCallback.login(userLoginDTO, eventListener);		
	}
	
	private void logout(IEventListener eventListener) {		
		UserAsyncCallback.logout(eventListener);		
	}

	private void removeUser(IEventListener eventListener) {		
		UserAsyncCallback.removeUser(eventListener);		
	}
	
	public void terminate() {
		
		fireEventGlobal(new RemoveEventRegistryListenerEvent(LoginEvent.class, this));
		fireEventGlobal(new RemoveEventRegistryListenerEvent(LogoutEvent.class, this));
		fireEventGlobal(new RemoveEventRegistryListenerEvent(AddUserEvent.class, this));
		fireEventGlobal(new RemoveEventRegistryListenerEvent(RemoveUserEvent.class, this));
	}

	/**
	 * @param eventListener
	 */
	private void updateUser(UserEditDTO userEditDTO, IEventListener eventListener) {
		
		UserAsyncCallback.update(userEditDTO, eventListener);
		
	}

}
